<?php

namespace support\base;

use think\facade\Db;

class BaseService
{
    static $table = '';
    static $pk = '';

    function __construct($table = '', $pk = 'id')
    {
        self::$table = $table;
        self::$pk = $pk;
    }

    /**
     * @return \think\db\BaseQuery
     */
    function query()
    {
        return Db::name(self::$table);
    }

    function getPk()
    {
        return self::$pk;
    }

    function getList($where = [], $pager = [], $order = [], $field = '*')
    {

        $query = $this->query()->where($where);
        if (!empty($order)) {
            $query->order($order);
        }
        if (!empty($pager) && isset($pager['page'])) {
            $pageNum = $pager['page'] ?: 1;
            $pageSize = $pager['size'] ?? 20;
            $query = $query->page($pageNum, $pageSize);
        }

        return $query->field($field)->select()->toArray();
    }

    function getCount($where = [])
    {
        return $this->query()->where($where)->count();
    }

    function isExist($where = [])
    {
        return $this->getCount($where) > 0;
    }

    function getOne($where = [], $order = [], $field = '*')
    {
        $query = $this->query()->where($where);
        if (!empty($order)) {
            $query = $query->order($order);
        }

        return $query->field($field)->find();
    }

    function getOneById($id, $pk = '', $field = '*')
    {
        if (!$pk) {
            $pk = $this->getPk();
        }
        return $this->query()
            ->where($pk, $id)
            ->field($field)
            ->find();
    }

    function insert(array $data)
    {
        return $this->query()->insert($data, true);
    }

    function insertAll(array $data)
    {
        return $this->query()->insertAll($data);
    }

    /**
     * @return int
     */
    function update($data = [], $where = [])
    {
        return $this->query()
            ->where($where)
            ->update($data);
    }

    /**
     * @return int
     */
    function updateByIds($data = [], $ids = [], $pk = '')
    {
        if (empty($pk)) {
            $pk = $this->getPk();
        }

        if (is_array($ids)) {
            return $this->query()
                ->whereIn($pk, $ids)
                ->update($data);
        }
        return $this->query()
            ->where($pk, $ids)
            ->update($data);
    }

    /**
     * @return int
     */
    function delete($where = [], $softDelete = [])
    {
        $query = $this->query()->where($where);
        if (!empty($softDelete)) {
            $query = $query->useSoftDelete($softDelete[0], $softDelete[1]);
        }
        return $query->delete();
    }

    function  deleteByIds($ids, $pk = '', $softDelete = [])
    {
        if (is_array($ids)) {
            $ids = [$ids];
        }
        if (!$pk) {
            $pk = $this->getPk();
        }
        $query = $this->query()->whereIn($pk, $ids);
        if (!empty($softDelete)) {
            $query = $query->useSoftDelete($softDelete[0], $softDelete[1]);
        }
        return $query->delete();
    }

    function max(string $field, array $where = [])
    {
        return $this->query()->where($where)->max($field);
    }
}
